//
//  FFShareImagesHandle.m
//  fashionbuyer
//
//  Created by ale tan on 2017/12/27.
//  Copyright © 2017年 ale tan. All rights reserved.
//

#import "FFShareImagesHandle.h"
#import "MBManager.h"

#define KCompressibilityFactor 1280.00

#define TimeOut 10

@implementation FFShareImagesHandle

#pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法
+ (UIImage *)getJPEGImagerImg:(UIImage *)image{
    CGFloat oldImg_WID = image.size.width;
    CGFloat oldImg_HEI = image.size.height;
    //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
    if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){
        //超过设置的最大宽度 先判断那个边最长
        if(oldImg_WID > oldImg_HEI){
            //宽度大于高度
            oldImg_HEI = (KCompressibilityFactor * oldImg_HEI)/oldImg_WID;
            oldImg_WID = KCompressibilityFactor;
        }else{
            oldImg_WID = (KCompressibilityFactor * oldImg_WID)/oldImg_HEI;
            oldImg_HEI = KCompressibilityFactor;
        }
    }
    UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
    NSData *dJpeg = nil;
    if (UIImagePNGRepresentation(newImg)==nil) {
        dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
    }else{
        dJpeg = UIImagePNGRepresentation(newImg);
    }
    return [UIImage imageWithData:dJpeg];
}

#pragma mark - 压缩一张网络图片 最大宽高1280 类似于微信算法
+ (void)getJPEGImagerImgUrl:(NSString *)imageUrl  Complete:(Complete)completeBlcok{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        UIImage *image = nil;
        if ([imageUrl isKindOfClass:[NSString class]]) {
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
            image = [UIImage imageWithData:data];
        }
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            if (image) {
                completeBlcok([self getJPEGImagerImg:image],YES);
            }
            else{
                completeBlcok(nil,NO);
            }
            
        });
    });
}

#pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法
+ (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr{
    NSMutableArray *newImgArr = [NSMutableArray new];
    for (int i = 0; i<imageArr.count; i++) {
        UIImage *newImg = [self getJPEGImagerImg:imageArr[i]];
        [newImgArr addObject:newImg];
    }
    return newImgArr;
}

#pragma mark - 压缩多张网络图片 最大宽高1280 类似于微信算法
+ (void)getJPEGImagerImgUrls:(NSArray *)imageUrls  Complete:(Complete)completeBlcok{
    NSMutableArray *imageArr = [NSMutableArray array];
    __block BOOL isTimeOut = NO;
    
    __block NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:TimeOut block:^(NSTimer * _Nonnull timer) {
        [MBManager hideAlert];
        
        isTimeOut = YES;
        
        if (imageArr.count) {
            completeBlcok([self getJPEGImagerImgArr:imageArr],YES);
        }
        else{
            completeBlcok(nil,NO);
        }
    } repeats:NO];
    
    [MBManager showLoading];
    
    dispatch_group_t group =  dispatch_group_create();
    
    for (int i=0; i<imageUrls.count; i++) {
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 执行1个耗时的异步操作
            NSString *imageUrl = imageUrls[i];
            if ([imageUrl isKindOfClass:[NSString class]]) {
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
                UIImage *image = [UIImage imageWithData:data];
                if (image) {
                    [imageArr addObject:image];
                }
            }
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        // 等前面的异步操作都执行完毕后，回到主线程...
        if (!isTimeOut) {
            [MBManager hideAlert];
            [timer invalidate];
            timer = nil;
            
            if (imageArr.count) {
                completeBlcok([self getJPEGImagerImgArr:imageArr],YES);
            }
            else{
                completeBlcok(nil,NO);
            }
        }
    });
    
}

#pragma mark - 压缩一张图片 自定义最大宽高
+ (UIImage *)getJPEGImagerImg:(UIImage *)image compressibilityFactor:(CGFloat)compressibilityFactor{
    CGFloat oldImg_WID = image.size.width;
    CGFloat oldImg_HEI = image.size.height;
    //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
    if(oldImg_WID > compressibilityFactor || oldImg_HEI > compressibilityFactor){
        //超过设置的最大宽度 先判断那个边最长
        if(oldImg_WID > oldImg_HEI){
            //宽度大于高度
            oldImg_HEI = (compressibilityFactor * oldImg_HEI)/oldImg_WID;
            oldImg_WID = compressibilityFactor;
        }else{
            oldImg_WID = (compressibilityFactor * oldImg_WID)/oldImg_HEI;
            oldImg_HEI = compressibilityFactor;
        }
    }
    UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
    NSData *dJpeg = nil;
    if (UIImagePNGRepresentation(newImg)==nil) {
        dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
    }else{
        dJpeg = UIImagePNGRepresentation(newImg);
    }
    return [UIImage imageWithData:dJpeg];
}
#pragma mark - 压缩多张图片 自定义最大宽高
+ (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr compressibilityFactor:(CGFloat)compressibilityFactor{
    NSMutableArray *newImgArr = [NSMutableArray new];
    for (int i = 0; i<imageArr.count; i++) {
        UIImage *newImg = [self getJPEGImagerImg:imageArr[i] compressibilityFactor:compressibilityFactor];
        [newImgArr addObject:newImg];
    }
    return newImgArr;
}
#pragma mark - 根据宽高压缩图片
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


@end
